/**
 * 打开图层iframe对话框
 * 
 * @param title
 *            标题
 * @param url
 *            iframe url shift:'left-top' ,
 */
function layerDialog(title, url) {
	var index = layer.open({
		type : 2,
		title : title,
		shadeClose : true,
		maxmin : true,
		fix : true,
		area : [ ($(window).width() - 10) + 'px',
				($(window).height() - 10) + 'px' ],
		border : [ 2, 0.3, '#000' ],
		moveOut : true,
		moveType : 1, scrollbar: false,
		content : url
	});
	//layer.autoArea(index);
	return index;
}
function layerMiniDialog(title, url) {
	var index = layer.open({
		type : 2,
		title : title,
		shadeClose : true,
		maxmin : false,
		fix : true,
		area : [ '768px', '512px' ],
		border : [ 2, 0.3, '#000' ],
		moveOut : true,scrollbar: false,
		moveType : 1,
		content : url
	});
	//layer.autoArea(index);
	return index;
}
function layerBtnDialog(title, url) {
	var index = layer.open({
		type : 2,
		title : title,
		shadeClose : true,
		maxmin : false,
		fix : true,scrollbar: false,
		area : [ '768px', '600px' ],
		border : [ 2, 0.3, '#000' ],
		moveOut : true,btns: 2,
		moveType : 1,btn: ['确定', '取消'],
		content :url
	});
	//layer.autoArea(index);
	return index;
}
function layerCustomDialog(title, url,_width,_height,callback) {
	var index = layer.open({
		type : 2,
		title : title,
		shadeClose : true,
		maxmin : false,
		fix : true,scrollbar: false,
		area : [ _width, _height ],
		border : [ 2, 0.3, '#000' ],
		moveOut : true,btns: 2,
		moveType : 1,btn: ['确定', '取消'],
		content :url,
	    yes: function(index){
	        //do something
	    	callback(index);
	    }
	});
	//layer.autoArea(index);
	return index;
}
// 复选框选择方法
function selectAll(box) {
	if (box == null) {
		return;
	}
	var checklist = document.getElementsByName("keys");
	if (box.checked) {
		for (var i = 0; i < checklist.length; i++) {
			if (checklist[i].disabled != true) {
				checklist[i].checked = true;
			}
		}
	} else {
		for (var i = 0; i < checklist.length; i++) {
			if (checklist[i].disabled != true) {
				checklist[i].checked = false;
			}
		}
	}
}
// 获取选中记录的ID
function getSelectedId() {
	var checklist = document.getElementsByName("keys");
	var ids = "";
	for (var i = 0; i < checklist.length; i++) {
		if (checklist[i].checked) {
			if (i == 0) {
				ids = checklist[i].value;
			} else {
				ids += "," + checklist[i].value;
			}
		}
	}
	return ids;
}
function getSelectedIdByName(keys) {
	var checklist = document.getElementsByName(keys);
	var ids = "";
	for (var i = 0; i < checklist.length; i++) {
		if (checklist[i].checked) {
			if (i == 0) {
				ids = checklist[i].value;
			} else {
				ids += "," + checklist[i].value;
			}
		}
	}
	return ids;
}
// 加载左边菜单
function loadLeftMenu() {
	var lyrload = layer.load(30);
	$.ajax({
				url : CUR_MENU_URL,
				dataType : "json",
				success : function(data) {
					layer.close(lyrload);
					if (data != null) {
						var shtml = [];
						for (var i = 0; i < data.length; i++) {
							var row = data[i];
							if (row.module_rank == "1") {
								shtml[shtml.length] = '<li id="menu' + row.mid
										+ '"><a href="' + CUR_ROOT
										+ row.module_url + '"><i class="fa '
										+ row.module_icon
										+ '"></i> <span class="nav-label">'
										+ row.module_name + '</span>';
								var subHtml = [];
								for (var j = 0; j < data.length; j++) {
									var rowSub = data[j];
									if (rowSub.module_rank == "2") {
										if (rowSub.parent_id == row.mid) {
											subHtml[subHtml.length] = '<li><a href="'
													+ CUR_ROOT
													+ rowSub.module_url
													+ '">'
													+ rowSub.module_name
													+ '</a></li>';
										}
									} else {
										continue;
									}
								}
								if (subHtml.length > 0) {
									shtml[shtml.length] = '<span class="fa arrow"></span></a><ul class="nav nav-second-level">'
											+ subHtml.join('') + '</ul></li>';
								} else {
									shtml[shtml.length] = '</a></li>';
								}

							} else {
								continue;
							}
						}
						$("#side-menu").append(shtml.join(""));

					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					layer.close(lyrload);
					layer.msg(textStatus);
				}
			});
}
// loadLeftMenu();

function getLocalTime(nS) {
	return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-")
			.replace(/日/g, " ").replace(/下午/g, "PM").replace(/上午/g, "AM");
}

/**
 * 价格个是化
 */
var formatCurrency = function(num) {
	if (isNaN(num)) {
		num = "0";
	}
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ','
				+ num.substring(num.length - (4 * i + 3));
	return (((sign) ? '' : '-') + num + '.' + cents);
}

/**
 * 时间戳转时间
 */
var timelongToDate = function(timelong) {
	var date = new Date(timelong * 1000);
	Y = date.getFullYear() + '-';
	M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
			.getMonth() + 1)
			+ '-';
	D = date.getDate() + ' ';
	h = date.getHours() + ':';
	m = date.getMinutes() + ':';
	s = date.getSeconds();
	return (Y + M + D + h + m + s);
};

function findSize(field_id) {
	var fileInput = $("#" + field_id)[0];
	byteSize = fileInput.files[0].size;
	return (Math.ceil(byteSize / 1024)); // Size returned in KB.
}
var friendly_timelong = function(timelong) {
	var date =timelongToDate(timelong);
	return friendly_time(date);
}
var friendly_time = function(_date) {
	try{
		if ('iOS' == plus.os.name) {
			var regEx = new RegExp("\\-","gi"); 
	        _date=_date.replace(regEx,"/"); 
       }
	}catch(e){
	}
	var d = new Date(Date.parse(_date));
	var time_stamp = d.getTime() / 1000;
	var now_d = new Date();
	var now_time = now_d.getTime() / 1000; //获取当前时间的秒数
	var f_d = new Date();
	f_d.setTime(time_stamp * 1000);
	var f_time = f_d.toLocaleDateString();

	var ct = now_time - time_stamp;
	var day = 0;
	if (ct < 0) {
		f_time = "【预约】" + f_d.toLocaleString();
	} else if (ct < 60) {
		f_time = Math.floor(ct) + '秒前';
	} else if (ct < 3600) {
		f_time = Math.floor(ct / 60) + '分钟前';
	} else if (ct < 86400) //一天
	{
		f_time = Math.floor(ct / 3600) + '小时前';
	} else if (ct < 604800) //7天
	{
		day = Math.floor(ct / 86400);
		if (day < 2)
			f_time = '昨天';
		else
			f_time = day + '天前';
	} else {
		//day = Math.floor(ct / 86400);
		f_time = _date.split(" ")[0];
	}
	return f_time;
};

//是否微信端
function is_weixin(){
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i)=="micromessenger") {
		return true;
 	} else {
		return false;
	}
}
/* 
* 获得时间差,时间格式为 年-月-日 小时:分钟:秒 或者 年/月/日 小时：分钟：秒 
* 其中，年月日为全格式，例如 ： 2010-10-12 01:00:00 
* 返回精度为：秒，分，小时，天
*/
function getDateDiff(startTime, endTime, diffType) {
	//将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式 
	startTime = startTime.replace(/\-/g, "/");
	endTime = endTime.replace(/\-/g, "/");
	//将计算间隔类性字符转换为小写
	diffType = diffType.toLowerCase();
	var sTime = new Date(startTime);	  //开始时间
	var eTime = new Date(endTime);  //结束时间
	//作为除数的数字
	var divNum = 1;
	switch (diffType) {
		case "second":
			divNum = 1000;
			break;
		case "minute":
			divNum = 1000 * 60;
			break;
		case "hour":
			divNum = 1000 * 3600;
			break;
		case "day":
			divNum = 1000 * 3600 * 24;
			break;
		default:
			break;
	}
	return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
}
/**
 * 时间转换
 * @param m
 * @returns
 */
function add0(m){return m<10?'0'+m:m }
/**
 * 时间转换
 * @param timestamp*1000
 * @returns {String}
 */
function format(timestamp)
{
  //timestamp是整数，否则要parseInt转换,不会出现少个0的情况
	var time = new Date(timestamp*1000);
	var year = time.getFullYear();
	var month = time.getMonth()+1;
	var date = time.getDate();
	var hours = time.getHours();
	var minutes = time.getMinutes();
	var seconds = time.getSeconds();
	return year+'-'+add0(month)+'-'+add0(date)+' '+add0(hours)+':'+add0(minutes)+':'+add0(seconds);
}
function delField(tabName,field,fieldValue){
	pageii = layer.confirm('确定删除？', function() {
		$.ajax({
			type : "post",
			url : "/Admin/Public/DelFieldName",
			data : {
				'tabName' : tabName,
				'fieldName' : field,
				'fieldValue' : fieldValue
			},
			dataType : "json",
			success : function(data) {
				if (data) {
					myTable.fnDraw();
					layer.msg("删除成功!");
				}
			},
			error : function(err) {
				layer.alert(err.info, 8);
			}
		});
	});
	return pageii;
}
function delallfield(tabName,field,fieldValue){
	$.ajax({
		type : "post",
		url : "/Admin/Public/AllDelFieldName",
		data : {
			'tabName' : tabName,
			'fieldName' : field,
			'fieldValue' : fieldValue
		},
		dataType : "json",
		success : function(data) {
			if (data) {
				myTable.fnDraw();
				layer.msg('批量删除成功。', {icon: 1});  
			}

		},
		error : function(err) {
			layer.alert(err.info, 8);

		}
	}); 
}

//Js过滤空格
function filterSpaceForJs(str){
	str = $.trim(str)
	str = str.replace(/ /g,'')
	return str;
}
//删除记录
function del(id,url){
	layer.confirm('确认删除？',function() {
		$.ajax({
			type : "post",
			url : url,
			data : {
				'id' : id,
			},
			dataType : "json",
			success : function(data) {
				layer.msg(data.msg);
				if (data.code==1) {
					location.reload();
				}
			},
			error : function(err) {
				layer.alert(err.info, 8);
			}
		});
	});
}

/*
 * 上传图片 后台专用
 * @access  public
 * @null int 一次上传图片张图
 * @elementid string 上传成功后返回路径插入指定ID元素内
 * @path  string 指定上传保存文件夹,默认存在public/upload/temp/目录
 * @callback string  回调函数(单张图片返回保存路径字符串，多张则为路径数组 )
 */
function GetUploadify(callback)
{
	var upurl ='/admin/upload?func='+callback;
	layer.open({
		type: 2,
		title: '上传图片',
		shadeClose: true,
		shade: false,
		maxmin: true, //开启最大化最小化按钮
		area: ['50%', '60%'],
		content: upurl
	});
}